//
//  KFDSettingsManager.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/9/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDSettingsManager.h"
#import "KFDAccountManager.h"
#import "NSMutableDictionary+checkForNil.h"
#import "KFDFileHelper.h"

@implementation KFDSettingsManager

+(KFDSettingsManager*)sharedManager
{
    static KFDSettingsManager* manager=nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        manager=[[KFDSettingsManager alloc] init];
    });
    return manager;
}

-(instancetype)init
{
    if (self=[super init]) {
        self.alarmSettingsOfAllTypes=[[NSMutableDictionary alloc] init];
        self.alarmSettingsOfAllTypes[@"hs300"]=[[KFDAlarmSettings alloc] init];
        self.alarmSettingsOfAllTypes[@"gold"]=[[KFDAlarmSettings alloc ]init];
        self.alarmSettingsOfAllTypes[@"silver"]=[[KFDAlarmSettings alloc ]init];
        self.alarmSettingsOfAllTypes[@"usoiln5"]=[[KFDAlarmSettings alloc ]init];
        self.alarmSettingsOfAllTypes[@"ukoilq5"]=[[KFDAlarmSettings alloc ]init];
        self.alarmSettingsOfAllTypes[@"usdx"]=[[KFDAlarmSettings alloc ]init];
        
        self.alarmRingtoneSettings=[[KFDAlarmRingtoneSettings alloc] init];
        
        self.auxiliaryFunctionSettings=[[KFDAuxiliaryFunctionSettings alloc] init];
    }
    return self;
}

-(NSURLSessionTask*)getAuxiliaryFunctionSettingsWithSuccessHandler:(KFDGetAuxiliaryFunctionSettingsSuccessHanler)successHandler failureHandler:(KFDRequestFailureHandler)failureHandler
{
    KFDClient* client=[KFDClient sharedClient];
    return [client getAuxiliaryFunctionSettingsWithSuccessHandler:^(KFDAuxiliaryFunctionSettings *settings) {
        self.auxiliaryFunctionSettings=settings;
        if (successHandler) {
            successHandler(settings);
        }
    } failureHandler:failureHandler];
}

-(NSURLSessionTask*)updateAuxiliaryFunctionSettingsWithSuccessHandler:(KFDUpdateAuxiliaryFunctionSettingsSuccessHandler)successHandler failureHandler:(KFDRequestFailureHandler)failureHandler
{
    KFDClient* client=[KFDClient sharedClient];
    return [client updateAuxiliaryFunctionSettings:self.auxiliaryFunctionSettings successHandler:successHandler failureHandler:failureHandler];
}

-(NSURLSessionTask*)getAlarmSettingsWithType:(NSString*)type successHandler:(KFDGetAlarmSettingsSuccessHandler)successHandler failureHandler:(KFDRequestFailureHandler)failureHandler
{
    KFDClient* client=[KFDClient sharedClient];
    return [client getAlarmSettingsWithType:type successHandler:^(KFDAlarmSettings *settings) {
        self.alarmSettingsOfAllTypes[type]=settings;
        if (successHandler) {
            successHandler(settings);
        }
    } failureHandler:failureHandler];
}

-(NSURLSessionTask*)updateAlarmSettingsWithType:(NSString*)type successHandler:(KFDUpdateAlarmSettingsSuccessHandler)successHandler failureHandler:(KFDRequestFailureHandler)failureHandler
{
    KFDClient* client=[KFDClient sharedClient];
    return [client updateAlarmSettings:self.alarmSettingsOfAllTypes[type] successHandler:successHandler failureHandler:failureHandler];
}

-(NSURLSessionTask*)getAlarmRingtoneSettingsWithSuccessHandler:(KFDGetAlarmRingtoneSettingsSuccessHandler)successHandler failureHandler:(KFDRequestFailureHandler)failureHandler
{
    KFDClient* client=[KFDClient sharedClient];
    return [client getAlarmRingtoneSettingsWithSuccessHandler:^(KFDAlarmRingtoneSettings *settings) {
        self.alarmRingtoneSettings=settings;
        if (successHandler) {
            successHandler(settings);
        }
    } failureHandler:failureHandler];
}

-(NSURLSessionTask*)updateAlarmRingtoneSettingsWithSuccessHandler:(KFDUpdateAlarmRingtoneSettingsSuccessHandler)successHandler failureHandler:(KFDRequestFailureHandler)failureHandler
{
    KFDClient* client=[KFDClient sharedClient];
    return [client updateAlarmRingtoneSettings:self.alarmRingtoneSettings successHandler:successHandler failureHandler:failureHandler];
}

-(void)saveMessageNotificationSettings
{
    [self.messageNotificationSettings saveToUserDefaults];
}

-(void)loadMessageNotificationSettings
{
    self.messageNotificationSettings=[KFDMessageNotificationSettings loadFromUserDefaults];
    if (!self.messageNotificationSettings) {
        self.messageNotificationSettings=[KFDMessageNotificationSettings new];
    }
}

//-(BOOL)loadSettingsForCurrentAccount
//{
//    KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
//    return [self loadSettingsForAccount:accountManager.phoneNumber];
//}
//
//-(BOOL)loadSettingsForAccount:(NSString *)phoneNumber
//{
//    NSUserDefaults* defaults=[NSUserDefaults standardUserDefaults];
//    NSDictionary* settingsOfAllAccounts=[defaults dictionaryForKey:@"settings"];
//    if (settingsOfAllAccounts) {
//        NSDictionary* settings=settingsOfAllAccounts[phoneNumber];
//        if (settings) {
//            self.forbidLiveOffWifi=((NSNumber*)settings[@"forbidLiveOffWifi"]).boolValue;
//            return YES;
//        }
//    }
//    return NO;
//}
//
//-(void)saveSettingsForCurrentAccount
//{
//    KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
//    NSUserDefaults* defaults=[NSUserDefaults standardUserDefaults];
//    NSMutableDictionary* settingsOfAllAccounts=[defaults dictionaryForKey:@"settings"].mutableCopy;
//    if (!settingsOfAllAccounts) {
//        settingsOfAllAccounts=[NSMutableDictionary dictionary];
//    }
//    NSMutableDictionary* settings=[NSMutableDictionary dictionary];
//    [settings setNotNilObject:@(self.forbidLiveOffWifi) forKey:@"forbidLiveOffWifi"];
//    [settingsOfAllAccounts setNotNilObject:settings forKey:accountManager.phoneNumber];
//    [defaults setObject:settingsOfAllAccounts forKey:@"settings"];
//}

@end

//---------------------------------------------------------------------------------------------------------------

@implementation KFDSettingsManager (BusinessRules)

-(BOOL)checkAlarmBaseValue:(NSString *)baseValue floatValue:(NSString *)floatValue illegalInputHandler:(KFDSettingsManagerIllegalInputHandler)handler
{
    if (!baseValue||baseValue.length==0) {
        handler(@"请输入基准价");
        return NO;
    }
    if (!floatValue||floatValue.length==0) {
        handler(@"请输入浮动值");
        return NO;
    }
    return YES;
}

-(BOOL)checkMinValue:(NSString *)minValue maxValue:(NSString *)maxValue illegalInputHandler:(KFDSettingsManagerIllegalInputHandler)handler
{
    if ((minValue&&minValue.length>0)||(maxValue&&maxValue.length>0)) {
        return YES;
    }else if(!minValue||minValue.length==0){
        handler(@"请输入最低价或最高价");
    }
    return NO;
}

@end